package com.acmedcare.framework.exchange.center.core.handler;

import com.acmedcare.framework.exchange.center.core.Const;
import com.acmedcare.framework.exchange.center.core.ErrorPluginContainer;
import com.acmedcare.framework.exchange.center.core.JmxContainerUtil;
import com.acmedcare.framework.exchange.center.core.PluginContainerMBean;
import com.acmedcare.framework.exchange.center.core.storage.PluginRepository;
import com.acmedcare.framework.exchange.center.core.storage.StageRepository;
import com.acmedcare.framework.exchange.center.entity.bean.Plugin;
import com.acmedcare.framework.exchange.center.entity.dto.PluginCommand;
import com.acmedcare.framework.kits.StringUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class PluginCommandHandler {

  final PluginRepository repository;
  final PluginContainerMBean pluginContainer;
  final ObjectMapper objectMapper;
  final StageRepository stageRepository;

  public PluginCommandHandler(PluginRepository repository,
      PluginContainerMBean container, ObjectMapper objectMapper,
      StageRepository stageRepository) {
    this.repository = repository;
    this.pluginContainer = container;
    this.objectMapper = objectMapper;
    this.stageRepository = stageRepository;
  }

  @SuppressWarnings("unused")
  public void receive(String message){

    log.info("接受到了插件指令:"+message);

   try {
     if (StringUtils.isEmpty(message)) {

       return;
     }

     PluginCommand command = objectMapper
         .readValue(message, PluginCommand.class);
     if (command.getPlugin() == null) {
       log.info("插件加载指令中尚未找到合适的插件,请更新Manager到最新后再试");
       return;
     }

     if (!command.getGroupName().equals(System.getenv(Const.GroupName))) {
       log.info(String.format("插件:%s指令加载环境{%s}，与当前环境{%s}不匹配，拒绝加载插件!", command.getPlugin().getName(),
           command.getGroupName(),
           System.getenv(Const.GroupName)));
       return;
     }

     switch (command.getStatusEnum()) {

       case LOADED:
         Plugin plugin = command.getPlugin();
         pluginContainer.load(plugin);
         try {
           repository.delete(command.getPlugin().getPluginId());
           repository.insert(plugin);
         } catch (Exception e) {
           log.warn("进行插件竞争插入操作失败:" + e.getMessage());
         }
         break;

       case UNLOAD:
         pluginContainer.unloadAsync(command.getPlugin());
         repository.delete(command.getPlugin().getPluginId());
         break;

       case RESTART:
         pluginContainer.restart(command.getPlugin().getName());
         PluginContainerMBean realContainer= JmxContainerUtil.GroupContainer.get(command.getPlugin().getName().toLowerCase());
         if (realContainer instanceof ErrorPluginContainer||stageRepository.getConfigs()==null){
           return;
         }

         stageRepository.getConfigs().forEach(p->realContainer.setEnv(p.getConfigName(),p.getConfigValue()));
     }
   }catch (Exception e){

     log.error("插件指令处理失败:"+message,e);
   }
  }
}
